package equation;

import java.util.Objects;

public abstract class Equation {

    // 算式类的成员变量
    // 都采用基本数据类型的封装类型，便于操作以及代码的可拓展性
    private Integer operandA;
    private Integer operandB;
    private Character operator = '?';

    public Equation(Integer operandA, Integer operandB) {
        this.operandA = operandA;
        this.operandB = operandB;
    }

    public Equation() {

    }

    // 这是一个抽象方法，所有子类需要实现这个方法, 方法的作用是计算算式的结果
    // 这个方法体现了多态
    public abstract Integer calculate();

    public Integer getOperandA() {
        return operandA;
    }

    public void setOperandA(Integer operandA) {
        this.operandA = operandA;
    }

    public Integer getOperandB() {
        return operandB;
    }

    public void setOperandB(Integer operandB) {
        this.operandB = operandB;
    }

    public Character getOperator() {
        return operator;
    }

    public void setOperator(Character operator) {
        this.operator = operator;
    }

    @Override
    public String toString() {
        return "" + this.operandA + " " + this.operator + " " + this.operandB + " = ";
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Equation equation = (Equation) o;
        return Objects.equals(operandA, equation.operandA) && Objects.equals(operandB, equation.operandB) && Objects.equals(operator, equation.operator);
    }

    @Override
    public int hashCode() {
        return Objects.hash(operandA, operandB, operator);
    }
}
